var packageJarName = "";
var isBundleName="";
$(document).ready(function() {
	
	$('#import_link').window({
		width:465,
		modal:true,
		minimizable:false,
		maximizable:false,
		collapsible:false
	});
	$('#import_link').window('close');  //关闭窗口
	
	
	
	$("#file").uploadify({
		       
            "uploader": BASE_URL_HTTP_INTERFACE + '/uploadFile',//url
           	//指定swf文件
            "swf":"../../../js/uploadify/uploadify.swf?id=<%=System.currentTimeMillis()%>",
            "cancelImg": "../../../js/uploadify/uploadify-cancel.png",
            "queueID": "content",//上传文件队列div的id
            "auto": false,//是否自动上传
            /*"buttonText": "浏览",*/
            "fileObjName ": "filedata",//传到服务器的参数名
            "width":"200px",
            "method": "POST",
            "fileTypeDesc": '请选择要上传的。jar文件！',
            "fileTypeExts": '*.jar',
            "onSelect":function(file){
            	$("#content").css("display","block");
            },
            "onUploadStart":function(file){
            	$("#uploadTable").css("display","block");
            },
            "onUploadSuccess" : function(file,data,response) {//上传完成时触发（每个文件触发一次）
            	//$("#uploadJar").height($("#fileUpload").height() +　20);
				var obj=eval("("+data+")");
				var flag=obj.success;
				//$("#fileUrl").val($("#fileUrl").val()+obj.fileUrl+","); 
				if(flag){
					$("#uploadTable tbody").append("<tr name='"+obj.fileName+"'><td>"+obj.fileName+"</td>"+
												"<td>上传成功</td>"+
												"<td><a href='javascript:void(0)' "+
												"onclick='deleteFile(\""+obj.fileName+"\")'>删除文件</a></td>" +
												"<td><input type=\"radio\" name=\"is_bundleName\" value='"+obj.fileName+"'/></td></tr>");
					//alert($("#packageJarName").val() + obj.fileName);
					packageJarName += (packageJarName != ""? ",":"") + obj.fileName;
					/*if(obj.fileName != "" || obj.fileName != null){
						packageName.push(obj.fileName);
					}*/
				}else{
					$("#uploadTable tbody").append("<tr name='"+obj.fileName+"'><td>"+obj.fileName+"</td>"+
												"<td>上传失败</td>"+
												"<td><a href='javascript:void(0)' "+
												"onclick='deleteFile(\""+obj.fileName+"\")'>删除文件</a></td></tr>");
					$.messager.alert('错误', '<p>'+ obj.msg + ', 上传接口部署包失败!</p>', 'error');
				}
				$("#packageJarName").val(packageJarName);
              	$.parser.parse($("#uploadTable"));
            },
            "onError" : function(){
            	$.messager.alert('错误', '<p>上传接口部署包失败!</p>', 'error');
            }

        });
	
});		


function import_link(){
	$('#import_link').window('open'); 
}

function closeImport_link(){
	isBundleName="";
	$("input[name='is_bundleName']:checked").map(function () {
		isBundleName = $(this).val();
    });
	//alert(is_bundleName);
	$('#import_link').window('close'); 
}

/*function deleteFiles(fileName){
	$.post(BASE_URL_HTTP_INTERFACE + '/deleteFile',{"fileNames":fileNames},function(data){
		if(data.success){
			alert(data.msg);
		}else{
			alert(data.msg);
		}
	});
}*/

function deleteFile(fileNames){
	//alert($("tr[name='"+fileNames+"'] td:eq(1)").text());
	if($("tr[name='"+fileNames+"'] td:eq(1)").text() == "上传失败"){
		$("tr[name='"+fileNames+"']").remove();
	}else{
		//fileNames+=fileName+',';
		$.post(BASE_URL_HTTP_INTERFACE + '/deleteFile',{"fileNames":fileNames},function(data){
			if(data.success){
				alert(data.msg);
			}else{
				alert(data.msg);
			}
		});
		$("tr[name='"+fileNames+"']").remove();
		var packageName = [];
		var packageJarName = "";
		//packageName.push(obj.fileName);
		if($("#packageJarName").val().indexOf(",") != -1){
			packageName = $("#packageJarName").val().split(","); 
			for(var i=0; i < packageName.length; i++){
				if(packageName[i] == fileNames){
					
				}else{
					packageJarName += (packageJarName != ""? ",":"") + packageName[i];
					//alert(packageJarName);
				}
			}
		}else{
			packageJarName = "";
			$("#uploadTable, #content").css("display", "none");
		}
		
		$("#packageJarName").val(packageJarName);
	}
	
}
